package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.SurfaceWaterIrrigationRate;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.sevenyear.excelanalyze.service.SurfaceWaterIrrigationRateService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/30 15:43
 */
public class SurfaceWaterIrrigationRateListener extends AnalysisEventListener<SurfaceWaterIrrigationRate> {

    private SurfaceWaterIrrigationRateService surfaceWaterIrrigationRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public SurfaceWaterIrrigationRateListener() {
    }

    public SurfaceWaterIrrigationRateListener(SurfaceWaterIrrigationRateService surfaceWaterIrrigationRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.surfaceWaterIrrigationRateService = surfaceWaterIrrigationRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(SurfaceWaterIrrigationRate surfaceWaterIrrigationRate, AnalysisContext analysisContext) {
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",surfaceWaterIrrigationRate.getLandform()).eq("clay_thickness",surfaceWaterIrrigationRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        surfaceWaterIrrigationRate.setPartitionId(partitionBaseInfo.getId());
        surfaceWaterIrrigationRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        surfaceWaterIrrigationRate.setAreaCode(surfaceWaterIrrigationRate.getArea()+surfaceWaterIrrigationRate.getPartitionCode());
        UpdateWrapper<SurfaceWaterIrrigationRate> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("area_code",surfaceWaterIrrigationRate.getAreaCode()).eq("year_record",surfaceWaterIrrigationRate.getYearRecord());
        surfaceWaterIrrigationRateService.saveOrUpdate(surfaceWaterIrrigationRate,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
